package edu.harding.tictactoe;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.harding.tictactoe.R;

public class AndroidTicTacToeActivity extends Activity {
	private TicTacToeGame mGame;
	private Button[] mBoardButtons;
	private TextView mInfoTextView;
	private boolean mGameOver = false;
	private int computerWins,humanWins,ties;
	private TextView mHumanScoreTextView;
	private TextView mComputerScoreTextView;
	private TextView mTieScoreTextView;
	
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mBoardButtons = new Button[TicTacToeGame.BOARD_SIZE];
		mBoardButtons[0] = (Button) findViewById(R.id.one);
		mBoardButtons[1] = (Button) findViewById(R.id.two);
		mBoardButtons[2] = (Button) findViewById(R.id.three);
		mBoardButtons[3] = (Button) findViewById(R.id.four);
		mBoardButtons[4] = (Button) findViewById(R.id.five);
		mBoardButtons[5] = (Button) findViewById(R.id.six);
		mBoardButtons[6] = (Button) findViewById(R.id.seven);
		mBoardButtons[7] = (Button) findViewById(R.id.eight);
		mBoardButtons[8] = (Button) findViewById(R.id.nine);		
		 
        mInfoTextView = (TextView) findViewById(R.id.information); 
        mHumanScoreTextView = (TextView) findViewById(R.id.player_score);
        mComputerScoreTextView = (TextView) findViewById(R.id.computer_score);
        mTieScoreTextView = (TextView) findViewById(R.id.tie_score);

		mGame = new TicTacToeGame();
		startNewGame();
	}

	private void startNewGame() {
		mGameOver = false;
		mGame.clearBoard();
		// Reset all buttons
		for (int i = 0; i < mBoardButtons.length; i++) {
			mBoardButtons[i].setText("");
			mBoardButtons[i].setEnabled(true);
			mBoardButtons[i].setOnClickListener(new ButtonClickListener(i));
			// Human goes first
			mInfoTextView.setText("You go first.");
		}
	}

	private void setMove(char player, int location) {
		mGame.setMove(player, location);
		mBoardButtons[location].setEnabled(false);
		mBoardButtons[location].setText(String.valueOf(player));
		if (player == TicTacToeGame.HUMAN_PLAYER)
			mBoardButtons[location].setTextColor(Color.rgb(0, 200, 0));
		else
			mBoardButtons[location].setTextColor(Color.rgb(200, 0, 0));
	}

	// Handles clicks on the game board buttons
	private class ButtonClickListener implements OnClickListener {
		int location;

		public ButtonClickListener(int location) {
			this.location = location;
		}

		public void onClick(View view) {
			if(mGameOver){
				for(int i = 0; i<mBoardButtons.length; i++){
					mBoardButtons[i].setEnabled(false);
				}
				return;
			}
			if (mBoardButtons[location].isEnabled()) {
				setMove(TicTacToeGame.HUMAN_PLAYER, location);
				// If no winner yet, let the computer make a move
				int winner = mGame.checkForWinner();
				if (winner == 0) {
					mInfoTextView.setText("It's Android's turn.");
					int move = mGame.getComputerMove();
					setMove(TicTacToeGame.COMPUTER_PLAYER, move);
					winner = mGame.checkForWinner();
				}
				if (winner == 0)
					mInfoTextView.setText("It's your turn.");
				else if (winner == 1) {
					mInfoTextView.setText(R.string.result_tie);
					mGameOver = true;
					ties++;
					mTieScoreTextView.setText(String.valueOf(ties));
				} else if (winner == 2){
					mInfoTextView.setText(R.string.result_human_wins);
					humanWins++;
					mGameOver = true;
					mHumanScoreTextView.setText(String.valueOf(humanWins));
				}
				else{
					mInfoTextView.setText(R.string.result_computer_wins);
					computerWins++;
					mGameOver = true;
					mComputerScoreTextView.setText(String.valueOf(computerWins));
				}
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add("New Game");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		startNewGame();
		return true;
	}

}